
package com.compuvest.tld;

// <editor-fold defaultstate="collapsed" desc="import">
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;

import com.compuvest.web.Category;
import com.compuvest.web.Items;
import com.compuvest.web.Preference;
import java.net.URLEncoder;
// </editor-fold>

public class LeftMenu extends TagSupport {

// <editor-fold defaultstate="collapsed" desc="init">
    private String pageName;
    private int categoryID;
    private int subcategoryID;
    private int lowPrice;
    private int hiPrice;
    private String pageUrl;
    private StringBuilder menu;
    private Items items;
    private String popUrl;
    private Map<Integer,String> popData;
// </editor-fold>

    public void setPageName(String pageName)            { this.pageName = pageName; }
    public void setCategoryID(int categoryID)           { this.categoryID = categoryID; }
    public void setSubcategoryID(int subcategoryID)     { this.subcategoryID = subcategoryID; }
    public void setLowPrice(int lowPrice)               { this.lowPrice = lowPrice; }
    public void setHiPrice(int hiPrice)                 { this.hiPrice = hiPrice; }
    public void setPageUrl(String pageUrl)              { this.pageUrl = pageUrl; }
    public void setItems(Items items)                   { this.items = items; }
    public void setPopUrl(String popUrl)                { this.popUrl = popUrl; }
    public void setPopData(Map<Integer,String> popData)	{ this.popData = popData; }

// <editor-fold defaultstate="collapsed" desc="doStartTag()">
    @Override
    public int doStartTag() throws JspException {

        String thisURL = "./";//(String)pageContext.getSession().getAttribute("thisURL");
//        if(thisURL == null) {
//            thisURL = pageContext.getServletContext().getInitParameter("http");
//        }

        menu = new StringBuilder(11000);
        try {
            menu.append("<div id='cv_menu'><div class='top'></div>");
            menu.append("<div id='cv_wm'>");
            // category
            if(categoryID == 0) {
                menu.append("<div class='hd'>").append(pageName).append("</div>");
                menu.append("<ul>");
                for(Category catMenu : items.getCategories()) {
                    menu.append("<li/>&raquo;<a href='").append(pageUrl).append("&cid=").append(catMenu.getCategoryID());
                    menu.append("'>").append(catMenu.getName()).append("</a> (").append(catMenu.getItemsInCategory()).append(")");
                }
                menu.append("</ul>");
            }
            // subcategory
            else if(categoryID != 0 && subcategoryID == 0) {
                menu.append("<div class='hd'>").append(items.getCategoryName()).append("</div>");
                menu.append("<ul>");
                for(Category scatMenu : items.getSubcategories()) {
                    menu.append("<li/>&raquo;<a href='").append(pageUrl).append("&cid=").append(categoryID);
                    menu.append("&sid=").append(scatMenu.getCategoryID());
                    menu.append("'>").append(scatMenu.getName()).append("</a> (").append(scatMenu.getItemsInCategory()).append(")");
                }
                menu.append("</ul>");
            }
            // preference
            else if(categoryID != 0 && subcategoryID != 0) {
                menu.append("<div class='hd'>Search by preference</div>");

		// remove price range
		StringBuilder remove = new StringBuilder();
                if(lowPrice != 0 && hiPrice != 0) {
                    remove.append("<li>&raquo; <a href='").append(thisURL).append("?pg=0&cid=").append(categoryID);
                    remove.append("&sid=").append(subcategoryID).append("&lop=0&hip=0").append(popUrl);
                    remove.append("'>").append(String.format("$%,.2f - $%,.2f", (float)lowPrice, (float)hiPrice)).append(" <font color='red'>(X)</font></a></li>");
                }
		// remove preference
		if(popData != null && popData.size() > 0) {
                    for(int key : popData.keySet()) {
//                    Iterator en = popData.keySet().iterator();
//                    while(en.hasNext()) {
//			int key = (Integer)en.next();

                        remove.append("<li>&raquo; <a href='").append(thisURL).append("?pg=0&cid=").append(categoryID);
                        remove.append("&sid=").append(subcategoryID).append("&lop=").append(lowPrice).append("&hip=").append(hiPrice);
                        remove.append("&p").append(key).append(popUrl);
                        remove.append("' title='Remove ").append(popData.get(key)).append("'>").append(popData.get(key)).append(" <font color='red'>(X)</font></a></li>");
                    }
                }
		if(remove.length() != 0) {
                    menu.append("<ul>").append(remove).append("</ul>");
                    menu.append("<div style='border-bottom:1px solid silver;'></div>");
		}

                menu.append("<nobr>Price Range</nobr>");
                menu.append("<div>");
                menu.append("$<input type='text' name='lop' value='").append(lowPrice).append("'/> - ");
                menu.append("$<input type='text' name='hip' value='").append(hiPrice).append("'/>");
                menu.append(" <input type='submit' name='range' value='Go'/>");
                menu.append("</div>");

                ArrayList<Preference> preferenceArray = items.getPreference();
                for(int indexPreference = 0; indexPreference < preferenceArray.size(); indexPreference++) {

                   List<String> preferenceBlock = preferenceArray.get(indexPreference).getBlock();
                   if(preferenceBlock == null || preferenceBlock.size() == 1) continue; // if 1 no need

                    menu.append("<hr/>");

                    menu.append("<nobr>").append(preferenceArray.get(indexPreference).getTitle()).append("</nobr>");

                    StringBuilder hide = new StringBuilder();
                    menu.append("<ul>");
                    for(int idxName = 0; idxName < preferenceBlock.size(); idxName++) {
                        menu.append("<li/>&raquo;<a href='").append(pageUrl);
                        menu.append("&cid=").append(categoryID).append("&sid=").append(subcategoryID);
                        menu.append("&lop=").append(lowPrice).append("&hip=").append(hiPrice);
                        menu.append("&p").append(indexPreference).append("=");
                        menu.append(URLEncoder.encode(preferenceBlock.get(idxName), "UTF-8")).append(popUrl).append("'>");
                        menu.append(preferenceBlock.get(idxName)).append("</a> (");
                        menu.append(preferenceArray.get(indexPreference).getCount(preferenceBlock.get(idxName)));
                        menu.append(")");

                        // view less 5
                        if(idxName == 5) {
                            menu.append("<li class='mr' onClick='cv_togls(").append(indexPreference).append(",1)' id='b");
                            menu.append(indexPreference).append("1' style='display:block;'/>More ...");
                            menu.append("</ul>");
                            menu.append("<ul id='b").append(indexPreference).append("0' style='display:none;'>");

                            hide.append("<li class='hd' onClick='cv_togls(").append(indexPreference).append(",0)'>Hide</li>");
                        }
                    }
                    menu.append(hide);
                    menu.append("</ul>");
                }
            }
            menu.append("</div><div class='bot'></div>");
            menu.append("</div>");

            pageContext.getOut().print(menu.toString());
//            pageContext.getOut().print(menu.toString() +"<br>"+menu.length()+" t="+(int)(System.currentTimeMillis()-startEvent)+" lp="+lowPrice+" hp="+hiPrice);
        }
        catch(java.io.IOException e) {
            throw new JspTagException("LeftMenu: " + e.getMessage());
        }

        return SKIP_BODY;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="doEndTag()">

    @Override
    public int doEndTag() {
        return EVAL_PAGE;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="release()">

    @Override
    public void release() {
        pageName = null;
        pageUrl = null;
        menu = null;
        items = null;
        popUrl = null;
        popData = null;
    }
// </editor-fold>
}
